package br.com.FII.utils;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItem;
import javax.faces.context.FacesContext;

public class JSFUtil {

	public static void addErrorMessage( Exception ex, String defaultMsg ) {
		String msg = ex.getLocalizedMessage();
		if ( msg != null && msg.length() > 0 ) {
			addErrorMessage( msg );
		} else {
			addErrorMessage( defaultMsg );
		}
	}

	public static void addErrorMessages( List<String> messages ) {
		for ( String message : messages ) {
			addErrorMessage( message );
		}
	}

	public static void addErrorMessage( String msg ) {
		FacesMessage facesMsg = new FacesMessage( FacesMessage.SEVERITY_ERROR, msg, msg );
		FacesContext.getCurrentInstance().addMessage( null, facesMsg );
		FacesContext.getCurrentInstance().validationFailed(); // Invalidate JSF page if we raise an error message

	}

	public static void addSuccessMessage( String msg ) {
		FacesMessage facesMsg = new FacesMessage( FacesMessage.SEVERITY_INFO, msg, msg );
		FacesContext.getCurrentInstance().addMessage( "successInfo", facesMsg );
	}

	public static Throwable getRootCause( Throwable cause ) {
		if ( cause != null ) {
			Throwable source = cause.getCause();
			if ( source != null ) {
				return getRootCause( source );
			} else {
				return cause;
			}
		}
		return null;
	}

	public static boolean isValidationFailed() {
		return FacesContext.getCurrentInstance().isValidationFailed();
	}

	public static boolean isDummySelectItem( UIComponent component, String value ) {
		for ( UIComponent children : component.getChildren() ) {
			if ( children instanceof UISelectItem ) {
				UISelectItem item = (UISelectItem) children;
				if ( item.getItemValue() == null && item.getItemLabel().equals( value ) ) {
					return true;
				}
				break;
			}
		}
		return false;
	}
}
